package com.easylinkin.linkappapi.meterbilling.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easylinkin.linkappapi.common.utils.excel.ExcelConstant;
import com.easylinkin.linkappapi.common.utils.excel.ExcelTools;
import com.easylinkin.linkappapi.common.utils.io.OutputStreamUtil;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import java.util.stream.Collectors;

import com.easylinkin.linkappapi.meterbilling.mapper.RechargeRecordMapper;
import com.easylinkin.linkappapi.meterbilling.entity.RechargeRecord;
import com.easylinkin.linkappapi.meterbilling.service.RechargeRecordService;
import org.springframework.util.Assert;

@Service
public class RechargeRecordServiceImpl extends ServiceImpl<RechargeRecordMapper, RechargeRecord> implements RechargeRecordService {
    @Resource
    LinkappUserContextProducer linkappUserContextProducer;

    /**
     * 分页
     * @param page
     * @param rechargeRecord
     * @return
     */
    @Override
    public Page getMeterDeviceConfigList(Page page, RechargeRecord rechargeRecord){
        if(rechargeRecord.getFunctionFdentification() != null && rechargeRecord.getFunctionFdentification().equals(1)){
            String phone = linkappUserContextProducer.getCurrentUsername();
            rechargeRecord.setPhone(phone);
        }else{
            String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
            rechargeRecord.setTenantId(tenantId);
        }
        page.setRecords(baseMapper.getRechargeRecordList(page,rechargeRecord));
        return page;
    }


    /***
     * 获取水电表费用
     * @param rechargeRecord
     * @return
     */
    @Override
    public Map<String,Double> getMeterDeviceConfigCost(RechargeRecord rechargeRecord){
        if(rechargeRecord.getFunctionFdentification() != null && rechargeRecord.getFunctionFdentification().equals(1)){
            String phone = linkappUserContextProducer.getCurrentUsername();
            rechargeRecord.setPhone(phone);
        }else{
            String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
            rechargeRecord.setTenantId(tenantId);
        }
        Map<String,Double> resultMap=new HashMap<>();
        List<Map<String, Double>> meterDeviceConfigCost = baseMapper.getMeterDeviceConfigCost(rechargeRecord);
        if(meterDeviceConfigCost == null || meterDeviceConfigCost.size()<=0){
            return resultMap;
        }
        meterDeviceConfigCost.stream().forEach(m->{
            resultMap.put(String.valueOf(m.get("type")),m.get("price"));
        });
        return resultMap;
    }


    /***
     * 导出
     * @param rechargeRecord
     * @param request
     * @param response
     */
    @Override
    public void exportData(RechargeRecord rechargeRecord, HttpServletRequest request, HttpServletResponse response) {
        String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
        rechargeRecord.setTenantId(tenantId);
        List<RechargeRecord> rechargeRecordList = baseMapper.getRechargeRecordList(null,rechargeRecord);
        Assert.notEmpty(rechargeRecordList,"根据条件查得需要导出的数据为空");
        String keyValue = "房号编码:roomCode,房间:roomName,住户:name,手机号码:phone,类型:typeName,费用类型:rechargeTypeName,支付方式:payTypeName,金额:priceName,支付时间:payTimeName,操作时间:createTimeName,操作人:operatorName";
        String title = "账单记录";
        String fileName = title + ".xls";
        try {
            List<String> stringList = new ArrayList<String>();
            OutputStream outputStream = OutputStreamUtil
                    .getOutputStream(request, response, fileName);
            ExcelTools.exportBankExcelV2(outputStream, keyValue, rechargeRecordList, ExcelConstant.XLS, title, stringList);
            response.flushBuffer();
            outputStream.close();
        } catch (IOException e) {
            log.error("excel导出失败");
            throw new RuntimeException("excel导出失败！IOException异常" + e.getMessage());
        } catch (Exception e) {
            log.error("excel导出失败");
            throw new RuntimeException("excel导出失败！" + e.getMessage());
        }
    }
}


